<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <link rel="stylesheet" href="./lib/bootstrap3.3.7.css">
    <script src="./lib/moment.2.24.0.js"></script>
    <script src="./lib/vue2.4.4.js"></script>
</head>

<body>
    <div id="app">

        <div class="panel panel-primary">
            <div class="panel-heading">
                <h4>添加品牌</h4>
            </div>
            <div class="panel-body form-inline">
                <label>Name:</label>
                <input v-model="name" @keyup.enter="addPinpai" type="text" class="form-control" placeholder="输入内容,回车添加">
                <label>搜索:</label>
                <input v-model="keywords" v-focus type="text" class="form-control" placeholder="搜索">
            </div>
        </div>

        <table class="table table-bordered table-hover table-striped">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Name</th>
                    <th>Ctime</th>
                    <th>Operation</th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="item in search(keywords)" :key="item.id">
                    <td>{{item.id}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.ctime}}</td>
                    <td>
                        <a href="#" @click.prevent="del(item.id)">删除</a>
                    </td>
                </tr>
            </tbody>
        </table>

    </div>

    <script>
        Vue.directive('focus', {
            inserted: function(el) {
                el.focus();
            }
        });
        var FORMAT_TIME_STR = 'YYYY-MM-DD HH:mm:ss';
        var vm = new Vue({
            el: '#app',
            data: {
                name: '',
                keywords: '',
                list: [
                    { id: 1, name: '奔驰', ctime: moment('20180512', 'YYYYMMDD').format(FORMAT_TIME_STR) },
                    { id: 2, name: '宝马', ctime: moment('20171212', 'YYYYMMDD').format(FORMAT_TIME_STR) }
                ]
            },
            methods: {
                addPinpai() {
                    if (this.name.replace(/(^\s*)|(\s*$)/g, "").length === 0) return;
                    var maxId = -1;
                    this.list.forEach((item)=>{
                        maxId = Math.max(item.id, maxId);
                    });
                    this.list.push({
                        id: maxId,
                        name: this.name,
                        ctime: moment().format(FORMAT_TIME_STR)
                    });
                    this.name = '';
                },
                del(_id) {
                    // ====== [方法一] ======
                    // var delIndex = -1;
                    // for(item of this.list) {
                    //     delIndex++;
                    //     if(_id == item.id) {
                    //         break;
                    //     }
                    // }
                    // this.list.splice(delIndex, 1);
                    // ====== [方法二] ======
                    // this.list.some((item, index)=>{
                    //     if (item.id == _id) {
                    //         this.list.splice(index, 1);
                    //         return;
                    //     }
                    // });
                    // ====== [方法三] ======
                    var delIndex = this.list.findIndex(item => {
                        if (_id == item.id) {
                            return true;
                        }
                    });
                    this.list.splice(delIndex, 1);
                },
                search(_keywords) {
                    if (_keywords.replace(/(^\s*)|(\s*$)/g, "").length === 0) return this.list;
                    return this.list.filter((item)=>{
                        // if (item.name.indexOf(_keywords) != -1) {
                        //     return true;
                        // }
                        // ------ 或者使用 `includes` 这个新的API
                        // if (item.name.includes(_keywords)) {
                        //     return true;
                        // }
                        // ------ 直接return这个if判断
                        return item.name.includes(_keywords);
                    });
                }
            }
        });
    </script>
</body>

</html>